﻿using System.Collections.Generic;
using System.Web.UI;

namespace BoldBrick.SharePoint.CommonControls.Utils
{
	public static class ControlHelper
	{
		/// <summary>
		/// Gets all controls which inherited from <typeparamref name="TClass"/>
		/// </summary>
		/// <typeparam name="TClass">Type of control</typeparam>
		/// <param name="parentControl">Parent control of controls</param>
		/// <returns>Returns all controls which inherited from <typeparamref name="TClass"/></returns>
		public static List<TClass> GetControls<TClass>(Control parentControl) where TClass : Control
		{
			List<TClass> controls = new List<TClass>();

			FindControls(parentControl, controls);

			return controls;
		}

		private static void FindControls<TClass>(Control parentControl, List<TClass> controls) where TClass : Control
		{
			foreach (Control control in parentControl.Controls)
			{
				if (control is TClass)
				{
					controls.Add((TClass)control);
				}

				FindControls(control, controls);
			}
		}
	}
}
